package com.sbr.seb.wicket.page.administration.user;

import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.validation.validator.StringValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sbr.seb.entity.User;
import com.sbr.seb.wicket.page.administration.AdministrationBasePage;

public class EditUser extends AdministrationBasePage {

	private static final long serialVersionUID = 1L;
	protected static final Logger logger = LoggerFactory.getLogger(EditUser.class);

	public EditUser() {
		super();
		add(new FeedbackPanel("feedback"));
		add(new UserForm("form"));
	}

	public EditUser(final User user) {
		super();
		add(new FeedbackPanel("feedback"));
		add(new UserForm("form", user));
	}


	private class UserForm extends Form<User> {

		private static final long serialVersionUID = 1L;

		public UserForm(String id) {
			super(id);
			setDefaultModel(new CompoundPropertyModel<User>(new User()));
			init();
		}
		
		public UserForm(String id, User user) {
			super(id);
			setDefaultModel(new CompoundPropertyModel<User>(user));
			init();
		}

		private void init() {

			TextField<String> firstName = new TextField<String>("name");
			firstName.setRequired(true);
			firstName.add(StringValidator.maximumLength(15));
			add(firstName);

			//            TextField lastName = new TextField("lastName");
			//            lastName.setRequired(true);
			//            lastName.add(StringValidator.maximumLength(20));
			//            add(lastName);

			//            TextField email = new TextField("email");
			//            email.add(StringValidator.maximumLength(150));
			//            email.add(EmailAddressValidator.getInstance());
			//            add(email);

			//            TextArea notes = new TextArea("notes");
			//            notes.add(StringValidator.maximumLength(500));
			//            add(notes);

			//            DropDownChoice group = new DropDownChoice("group");
			//            group.setChoices(new AbstractReadOnlyModel() {
			//                public Object getObject() {
			//                    List<String> l = new ArrayList<String>(3);
			//                    l.add("Friend");
			//                    l.add("Co-Worker");
			//                    l.add("Nemesis");
			//                    return l;
			//                }
			//            });
			//            add(group);

			
			
//			add(new Button("save") {
//				private static final long serialVersionUID = 1L;
//
//				public void onSubmit() {
//					User c = getForm().getModelObject();
//					//                    WicketApplication.get().getContactDao().save(c);
//					setResponsePage(ListUsers.class);
//				}
//			});
			add(new Button("cancel") {
				private static final long serialVersionUID = 1L;

				public void onSubmit() {
					setResponsePage(ListUsers.class);
				}
			}.setDefaultFormProcessing(false));
		}

		@Override
		protected void onSubmit() {
			logger.debug("onSubmit called");
//			User user = getModelObject();
			//                    WicketApplication.get().getContactDao().save(c);
			setResponsePage(ListUsers.class);
		}
		
	}

	//		final TextField<Object> categoryNameText = new RequiredTextField("categoryName", new PropertyModel(category, "name"));
	//		final TextField<Object> categoryDescriptionText = new TextField("categoryDescription",new PropertyModel(category, "description"));
	//
	//		final DropDownChoice<Category> categoryBox = new DropDownChoice<Category>("categoryBox", new PropertyModel(category,"upperCategory"), new LoadableDetachableModel<List<Category>>(){
	//			@Override
	//			protected List<Category> load() { 
	//				return categoryDao.findAll();
	//			}
	//		},  new ChoiceRenderer<Category>("name", "id"));


	//		Form form = new Form("addCategoryForm") {
	//
	//			@Override
	//			protected void onSubmit() {
	//				logger.debug("onSubmit called");
	////				administrationService.addCategory(category);
	////				setResponsePage(AddCategoryPage.class);
	//			}
	//		};
	//		form.add(categoryNameText);
	//		form.add(categoryDescriptionText);
	//		form.add(categoryBox);

	//		add(form);
	//		add(new FeedbackPanel("feedbackPanel"));
	//	}

}
